package com.woniuxy.three.pms.root.controller;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniuxy.three.pms.bean.RootBean;
import com.woniuxy.three.pms.root.service.IRootService;

/**
 * 
 * @ProjectName: RoomManagerSystem
 * @Package: com.woniuxy.three.pms.root.controller
 * @ClassName: RootController
 * @author 郑杰  
 * @Description:操作员表示层
 * @date 2019年6月21日  下午2:17:37
 * @Version:1.0
 * @See：1.0
 */
@Controller
public class RootController {
	@Autowired
	private IRootService root;
	@ResponseBody
	@RequestMapping(value="/root",method=RequestMethod.GET)
	public List<RootBean> selectByPrimaryKey() {
		List<RootBean> list = root.selectByPrimaryKey();
		return list;
	}

	
	@ResponseBody
	@RequestMapping(value="/root/{rootid}",method=RequestMethod.GET)
	public RootBean selectById(@PathVariable("rootid")Integer rid) {
		RootBean root1 = root.findById(rid);
		return root1;
	}
	

	@ResponseBody
	@RequestMapping(value="/root/{username}/{password}",method=RequestMethod.POST)
	public boolean login(@PathVariable("username")String name,@PathVariable("password")String password) {

		Subject subject = SecurityUtils.getSubject();
		UsernamePasswordToken token = new UsernamePasswordToken(name,password);
		try {
			subject.login(token);
		} catch (UnknownAccountException e) {
			//UnknownAccountException用户名不存在
			e.printStackTrace();
			return false;
		}catch(IncorrectCredentialsException e1) {
			e1.printStackTrace();
			return false;
		}catch (AuthenticationException ae) {
			ae.printStackTrace();
			return false;

		}
		return true;
	}
	@ResponseBody
	@RequestMapping(value="/root/delete/{id}",method=RequestMethod.DELETE)
	public int deleteByPrimaryKey(@PathVariable("id")Integer id) {
		int i = root.deleteByPrimaryKey(id);
		return i;
	}

	@ResponseBody
	@RequestMapping(value="/root",method=RequestMethod.POST)
	public int insert(@RequestBody RootBean record) {
		System.out.println(record);
		//盐值
	   ByteSource mobileSalt = ByteSource.Util.bytes(record.getName());
	   //加密后的密码
	   Object enResult = new SimpleHash("MD5", record.getPassword(), mobileSalt, 1024);
	   record.setPassword(enResult.toString());
	   System.out.println(record);
		int i = root.insert(record);
		
		return i;
	}

	@ResponseBody
	@RequestMapping(value="/root",method=RequestMethod.PUT)
	public int updateByPrimaryKey(@RequestBody RootBean record) {
		int i = root.updateByPrimaryKey(record);
		return i;
	}
}
